<?php
namespace App\Http\Controllers\Tongji;

use Illuminate\Http\Request;
use	Exception;

include_once ('../app/libary/Tongji/Config.inc.php');
include_once ('../app/libary/Tongji/LoginService.inc.php');
include_once ('../app/libary/Tongji/ReportService.inc.php');

use LoginService;
use ReportService;



class IndexController
{


    public function getAccountInfo(Request $request)
    {

            $method = 'trend/time/a';       //趋势分析报告
            $start_date = '20180101';       //所查询数据的起始日期
            $end_date = '20180108';         //所查询数据的结束日期
            $gran = 'day';                  //按天粒度
            $max_results = 0;               //返回所有条数
            $metrics = 'pv_count,visitor_count,new_visitor_count,trans_count';               //返回所有条数

//        $method = $request->input('method');
//        $metrics = $request->input('metrics');
//        $start_date = $request->input('start_date');
//        $end_date = $request->input('end_date');
//        $gran = $request->input('gran');

        $loginService = new LoginService(LOGIN_URL, UUID);

        // preLogin
        if (!$loginService->preLogin(USERNAME, TOKEN)) {
            exit();
        }

        // doLogin
        $ret = $loginService->doLogin(USERNAME, PASSWORD, TOKEN);
        if ($ret) {
            $ucid = $ret['ucid'];
            $st = $ret['st'];
        }
        else {
            exit();
        }
//        dd($method);

        $reportService = new ReportService(API_URL, USERNAME, TOKEN, $ucid, $st);
        // get site list

        $ret = $reportService->getSiteList();    //   获取用户站点列表
        $siteList = $ret['body']['data'][0]['list'];
dd($siteList);
        if (count($siteList) > 0) {
            $siteId = $siteList[0]['site_id'];

            $arr = array(
                'site_id' => $siteId,                   //站点ID
                'method' => $method,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'metrics' => $metrics,  //所查询指标为PV和UV
                'max_results' => 0,
                'gran' => $gran,
                'target' => -1,
            );

            $ret = $reportService->getData($arr);

//            $ret = $reportService->getData(array(
//                'site_id' => $siteId,                   //站点ID
//                'method' => 'trend/time/a',             //趋势分析报告
//                'start_date' => '20160501',             //所查询数据的起始日期
//                'end_date' => '20160531',               //所查询数据的结束日期
//                'metrics' => 'pv_count,visitor_count',  //所查询指标为PV和UV
//                'max_results' => 0,                     //返回所有条数
//                'gran' => 'day',                        //按天粒度
//            ));
//            echo $ret['raw'] . PHP_EOL;
            $all_info = json_decode($ret['raw'],true);

            return json_encode($all_info);
        }

// doLogout
//        $loginService->doLogout(USERNAME, TOKEN, $ucid, $st);


    }





}


class GetMacAddr{

    var $return_array = array(); // 返回带有MAC地址的字串数组
    var $mac_addr;

    function GetMacAddr($os_type){
        switch ( strtolower($os_type) ){
            case "linux":
                $this->forLinux();
                break;
            case "solaris":
                break;
            case "unix":
                break;
            case "aix":
                break;
            default:
                $this->forWindows();
                break;

        }


        $temp_array = array();
        foreach ( $this->return_array as $value ){

            if (
            preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value,
                $temp_array ) ){
                $this->mac_addr = $temp_array[0];
                break;
            }

        }
        unset($temp_array);
        return $this->mac_addr;
    }


    function forWindows(){
        @exec("ipconfig /all", $this->return_array);
        if ( $this->return_array )
            return $this->return_array;
        else{
            $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
            if ( is_file($ipconfig) )
                @exec($ipconfig." /all", $this->return_array);
            else
                @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
            return $this->return_array;
        }
    }



    function forLinux(){
        @exec("ifconfig -a", $this->return_array);
        return $this->return_array;
    }

}